import sys
import xlrd
import xlwt
import os
import time
from xlutils.copy import copy
p=os.path.dirname(__file__)
parent_path = os.path.dirname(p)
sys.path.append(parent_path)
from ulits.excelclass import Excel
from login.usersinfor import InformationAction
class Shoping_cart(object):
    def __init__(self,ID):
        self.ID=ID
    def check_goods(self):
        file_path = parent_path + '/data/goods_info.xls'
        excel=Excel(file_path)
        excel.find_data('商品信息')
        add=input('您可以输入您喜欢的产品编号加入购物车，返回上一级请输入Q/q:')
        ww=xlrd.open_workbook(file_path)
        sheet=ww.sheet_by_name('商品信息')
        l1=sheet.col_values(0)
        if add in l1:
            number1=input('请输入您要购买的数量：').strip()
            if number1.isdigit():
                number1=int(number1)
                row=l1.index(add)
                l2=sheet.row_values(row)#这一行的所有值
                if number1<=l2[3]:
                    l2[3]=number1
                    l2.append(self.ID)
                    file_path = parent_path + '/data/shopping_car.xls'
                    excel1=Excel(file_path)
                    excel1.add_data(l2)
                    print('添加购物车成功！')
                else:
                    print('库存不足')
            else:
                print('数量请输入数字')
        elif add.upper()=='Q':
            self.vive()
        else:
            print('您的输入有误')
    def check_shoping_car(self):#查看购物车，包括删除指定商品，
        #查看购物车
        file_path = parent_path + '/data/shopping_car.xls'
        ww1=xlrd.open_workbook(file_path)
        sheet1=ww1.sheet_by_name('购物车')
        lp=sheet1.col_values(5)
        for i in range(len(lp)):
            if self.ID==lp[i]:
                #print(i)
                print(sheet1.row_values(i))

    # def check_shoping_car(self):#查看购物车，包括删除指定商品，
    #     file_path = parent_path + '/data/shopping_car.xls'
    #     excel=Excel(file_path)
    #     excel.find_data('购物车')
        lchooice=['删除商品','清空购物车','结算','退出']
        for i,j in enumerate(lchooice,1):
            print(i,j)
        chooice1=input('请输入您的选择：')
        if chooice1=='1':
            input1=input('请输入您要删除的商品id,Q/q退出：')
            file_path = parent_path + '/data/shopping_car.xls'
            excel=Excel(file_path)
            ww1=xlrd.open_workbook(file_path)
            sheet1=ww1.sheet_by_name('购物车')
            if input1 in sheet1.col_values(0):
                excel.delete_data('购物车',input1)
                self.check_shoping_car()

            elif input1.upper()=='Q':
                self.check_shoping_car()
            else:
                print('输入有误！')
                self.check_shoping_car()
        elif chooice1=='2':
            self.empty()
            print('购物车已清空！')
            self.check_shoping_car()
        elif chooice1=='3':
            self.settle_accounts()
            self.check_shoping_car()
        elif chooice1=='4':
            self.vive()
        else:
            print('输入有误！！！')
    def settle_accounts(self):
        number2=input('您是否要进行结算n/N结算，q/Q返回上一级：')
        if number2.upper()=='N':
            file_path = parent_path + '/data/shopping_car.xls'
            excel=Excel(file_path)
            ww=xlrd.open_workbook(file_path)
            sheet=ww.sheet_by_name('购物车')
            price_list=sheet.col_values(2)#购物车中的商品价格
            number_list=sheet.col_values(3)#购物车中的商品数量
            id_list=sheet.col_values(4)#商家id
            sum=0
            for i in range(1,sheet.nrows):
                sum+=price_list[i]*number_list[i]
            
            infor=InformationAction(self.ID)
            list1=infor.get_Information()
            msm=list1[1]
            add=int(input('请选择你的收货地址：'))
            if add>len(msm):
                print('地址不存在！')
            else:
                add=add-1
            
            for i in range(1,sheet.nrows):
                lx=sheet.row_values(i)#购物车列表
                for k in msm[add][1:]:
                    lx.append(k)
                lx.append('已付款待发货')
                timenow=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
                lx.append(timenow)
                timemow1=int(time.time())
                order_id=id_list[i]+str(timemow1)
                lx.append(order_id)
                # print('购买成功您此次消费为：{}'.format(sum))
                file_path = parent_path + '/data/buy_order.xls'
                ww3=xlrd.open_workbook(file_path)
                sheet3=ww3.sheet_by_name('买家订单')
                excel3=Excel(file_path)
                excel3.add_data(lx)
            print('购买成功您此次消费为：{}'.format(sum))
            file_path = parent_path + '/data/goods_info.xls'
            ww6=xlrd.open_workbook(file_path)
            sheet2=ww6.sheet_by_name('商品信息') 
            l1=sheet2.col_values(0)#商品表的第一列
            newtable=copy(ww6)#商品表
            sheets=newtable.get_sheet(0)
            for i in range(1,sheet.nrows):#购物车列表
                lx=sheet.row_values(i)
                index=l1.index(lx[0])
                l8=[]
                l8=sheet2.col_values(3)#商品表中的库存行
                del l8[0]
                res = l8[index-1]-lx[3]
                sheets.write(index,3,res)
            file_path = parent_path + '/data/goods_info.xls'
            newtable.save(file_path)
            slist=['商品ID','商品名称','商品价格','商品数量','商家ID','买家ID']
            excel.delete_all_data('购物车',slist)
        elif number2.upper()=='Q':
            self.vive()
        else:
            print('输入有误')
    def empty(self):#清空购物车
        file_path = parent_path + '/data/shopping_car.xls'
        excel=Excel(file_path)
        slist=['商品ID','商品名称','商品价格','商品数量','商家ID','买家ID']
        excel.delete_all_data('购物车',slist)
    def myorder(self):
        file_path = parent_path + '/data/buy_order.xls'
        excel=Excel(file_path)
        excel.find_data('买家订单')
        num=input('返回,Q/q:').strip()
        if num.upper()=='Q':
            self.vive()
    def vive(self):
        print('''
            1.查看商品
            2.购物车
            3.我的订单
            4.个人信息
            5.退出''')
